package core;

import CommitLink.*;
import fileoperation.FileDeletion;
import gitobject.Blob;
import gitobject.Branch;
import repository.Repository;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

/**
 * @author shkstart
 * @create 2021-12-20 15:07
 */
public class JitBranch extends JitReset {


    public static Branch branch(String name) throws Exception {
        return new Branch(name);
    }

    public static void branch() throws IOException {
        Branch branch = new Branch();
        String headName = branch.getHead().substring(28);
        String branchPath = new File(".").getCanonicalPath() + File.separator+ ".jit" + File.separator + "refs" + File.separator + "heads";
        File f = new File(branchPath);
        File[] files = null;
        if(f != null) {
            files = f.listFiles();
        }
        for(File file:files){
            if(file.getName().equals(headName)){
                System.out.println("*" + headName);
                continue;
            }
            System.out.println(file.getName());
        }
    }

    public static void deleteBranch(String name) throws IOException {
        String branchPath = new File(".").getCanonicalPath() + File.separator+ ".jit" + File.separator + "refs" + File.separator + "heads" + File.separator + name;
        File file = new File(branchPath);
        FileDeletion.deleteFile(file);
        System.out.println("branch " + name + " has been removed ");
    }

    public static void checkout(String name) throws Exception {
        String branchPath = new File(".").getCanonicalPath() + File.separator+ ".jit" + File.separator + "refs" + File.separator + "heads" + File.separator + name;
        File file = new File(branchPath);
        Branch branch = new Branch();
        if(!file.exists()){
            System.out.println("branch " + name + " doesn't exist");
            return;
        }
        File HEAD = new File(new File(".").getCanonicalPath() + File.separator+ ".jit" + File.separator + "HEAD");
        FileOutputStream fos = new FileOutputStream(HEAD);
        String str = "ref: refs/heads/" + name;
        fos.write(str.getBytes(StandardCharsets.UTF_8));
        CommitLink commitLink = new CommitLink();
        LinkList list =  commitLink.getCommitList();
        String headPath =  branch.getHead();
        String id = branch.getValue(new File(headPath));
        list.switchLast(id);
        commitLink.setCommitList(list);
        commitLink.writeObject();
        JitReset.reset_hard("###" + id );
        fos.close();
    }




    //清空工作区未追踪文件

    public static void clear_work() throws Exception {
        //获得过滤后的文件列表
        ArrayList<File> list = inObjects();
        //逐一删除文件
        for (File del:list) {
            del.delete();
        }
    }



    static ArrayList<File> inObjects() throws Exception {
        String workArea = new File(".").getCanonicalPath();
        File file = new File(workArea);
        ArrayList<File> deleteList = new ArrayList<>();
        return dfsFindObjects(file, deleteList);
    }

    public static ArrayList<File> dfsFindObjects(File file, ArrayList<File> fileList) throws Exception {
        if (file.isFile() && !file.getAbsolutePath().contains(".jit")) {
            Blob blob = new Blob(file,"2");
            String id = blob.getKey();
            if (findBlob(id) != null) {
                fileList.add(file);
            }
        }
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File file1 : files) {
                    dfsFindObjects(file1, fileList);
            }
        }
        return fileList;
    }

    //根据前缀寻找ID
    public static String findBlob(String prefixes) throws IOException {
        String dicPart=prefixes.substring(0,2);
        String filePart=prefixes.substring(2,prefixes.length());
        File dic=new File(new File(".").getCanonicalPath() + File.separator+ ".jit"+File.separator+"objects"+File.separator+dicPart);
        if(dic == null){
            return null;
        }
        File[] fs=dic.listFiles();
        if(fs == null){
            return null;
        }
//        System.out.println(fs.length);
        for (File cur:fs) {
            //System.out.println(cur.getAbsolutePath());
            if(cur.getAbsolutePath().contains(filePart)){
                int index=cur.getAbsolutePath().lastIndexOf(dicPart);
                String temp= cur.getAbsolutePath().substring(index);
                return temp.substring(0,2)+temp.substring(3);
            }
        }
        return null;
    }
}
